/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.grnt.buddy;
import org.grnt.common.OptStatBean;

/**
 * This class describes a user entidy, and extends OptStatBean class. All this
 * class objects will be holds in context.
 * @author sayid
 */
public class UserBean extends OptStatBean{
	Long   userId;
	String loginName;
	String password;
	Long   rootGroupId;

	final class Init {
		String password;
	};

	Init init = new Init();
	/**
	 *
	 * @return User id.
	 */
	final public Long getUserId() {
		return this.userId;
	}

	/**
	 *
	 * @return Longin name.
	 */
	final public String getLoginName() {
		return this.loginName;
	}

	/**
	 *
	 * @return Password.
	 */
	final public String getPassword() {
		return this.password;
	}
	
	/**
	 *
	 * @return Buddy id of root group buddy.
	 */
	final public Long getRootGroupId() {
		return this.rootGroupId;
	}

	/**
	 *
	 * @param password New password.
	 * @param committed If it is true the update will be autocommit after changed.
	 * @throws org.grnt.common.OptStatBean.OptStateException
	 */
	final public void setPassword(String password, boolean committed) throws OptStateException {
		this.password = password;
		if (committed) this.commit();
	}

	/**
	 *
	 * Init a UserBean object.
	 * @param userId User id.
	 * @param loginName Login name.
	 * @param password Password.
	 * @param rootGroupId Buddy id of its root group buddy.
	 */
	final public void init
	(	Long   userId
	,	String loginName
	,	String password
	,	Long   rootGroupId
	) 
	{
		this.userId      = userId;
		this.loginName   = loginName;
		this.password    = init.password = password;
		this.rootGroupId = rootGroupId;
	}

	@Override
	public void commit() throws OptStateException {
		init.password = this.password;
	}

	@Override
	public void rollback() throws OptStateException {
		this.password = init.password;
	}

	@Override
	public boolean isDirty() {
		return this.password.equals(init.password);
	}
}
